﻿using HalconDotNet;
using Halcon二次开发.Model.储存每个Controller的单例对象;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Halcon二次开发.Controller.Controller基类;
using Halcon二次开发.Model;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.Model.工具区域的扩展类;
using System.Windows.Shapes;
using static System.Windows.Forms.LinkLabel;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using Halcon二次开发.View.卡尺;
using Halcon二次开发.Tools;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.View.图片显示弹窗;
using System.Reflection;

namespace Halcon二次开发.Controller.卡尺
{
    /// <summary>
    /// 精准测量卡尺
    /// </summary>
    public class Caliper
    {

        /// <summary>
        /// 该工具的线段对象
        /// </summary>
        List<HDrawingObjectEx> lines = new List<HDrawingObjectEx>();
        /// <summary>
        /// 卡尺界面
        /// </summary>
        PreciseCaliper form;
        /// <summary>
        /// 卡尺校验保存数据
        /// </summary>
        public string path;
        HTuple hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2;
        HObject ho_ROI_1;
        /// <summary>
        /// 该工具的线段对象
        /// </summary>
        HDrawingObjectEx line;
        /// <summary>
        /// 当前控件对象
        /// </summary>
        public HSmartWindowControl hw;

        /// 移动事件更改数据禁止
        /// </summary>
        bool check = false;
        /// <summary>
        /// 有没有放置
        /// </summary>
        bool putCheck = false;
        /// <summary>
        /// 模式选择
        /// </summary>
        ComboBox combox1;

        /// <summary>
        /// 位置-旋转-长度1-长度2
        /// </summary>
        NumericUpDown numericUpDown1, numericUpDown2, numericUpDown3, numericUpDown7, numericUpDown10;

        /// <summary>
        /// 标注图形,用于第一次画显示在模板的第一个元素上面
        /// </summary>
        public HObject ho_Biaozhu;

        /// <summary>
        /// 偏移量 - 第一个模板的数据  第一个绘制的线段数据 
        /// </summary>
        public double firstModel_y, firstModel_x, firstModel_phi;
        #region 控制参数
        /// <summary>
        /// 输入图像
        /// </summary>
        public HObject HideIntputImage;
        /// <summary>
        /// 输入的行-列-角度 坐标
        /// </summary>
        public double[] HideIntputArrayRows, HideIntputArrayColumns, HideIntputArrayPhis;
        /// <summary>
        /// 卡尺的位置  - 输出点 - 距离 
        /// </summary>
        public double HideIntputX, HideIntputY, HideIntputPhi, HideIntputLength1, HideIntputLength2, HideOutputX1, HideOutputY1, HideOutputX2, HideOutputY2;

        /// <summary>
        /// 输出的距离数组
        /// </summary>
        public double[] HideOutputArrayDistances;

        /// <summary>
        /// 卡尺的宽度,高度,对比度,平滑度
        /// </summary>
        public double HideIntputWidth = 0, HideIntputHeight = 0, HideIntputThreshold, HideIntputSigma;
        /// <summary>
        /// 颜色样式
        /// </summary>
        public string HideOutputBorderColor, HideOutputCenterConlor, HideOutputlineColor = "blue";
        //输出结果
        public Dictionary<HObject, bool> HideOutputResults = new Dictionary<HObject, bool>();
        //中心的row，column坐标
        public double[] HideOutputArrayRows, HideOutputArrayColumns;
        /// <summary>
        /// 所有线段的2个点的集合，二维数组
        /// </summary>
        public double[,] linesList;

        public HwindowFlowNodeUrseControl hwindowFlowNode;
        #endregion
        public Caliper(string path, PreciseCaliper form, HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            //获取当前工位
            hw = hwindowFlowNode.hSmartWindowControl1;
            this.hwindowFlowNode = hwindowFlowNode;
            this.path = path;
            this.form = form;
        }

        public Caliper()
        {
        }

        /// <summary>
        /// 画矩形
        /// </summary>
        /// <param name="hv_WindowHandle">句柄</param>
        /// <param name="ho_ROI_0">画的区域</param>
        public void DrawRecangle(NumericUpDown numericUpDown1, NumericUpDown numericUpDown2, NumericUpDown numericUpDown3, NumericUpDown numericUpDown7, NumericUpDown numericUpDown10,
            NumericUpDown numericUpDown4, NumericUpDown numericUpDown8, NumericUpDown numericUpDown9,
            NumericUpDown numericUpDown6, NumericUpDown numericUpDown5,
            TextBox textBox1, TextBox textBox2, TextBox textBox3, ComboBox combox1)
        {
            HOperatorSet.ClearWindow(hw.HalconWindow);
            HOperatorSet.DispObj(HideIntputImage,hw.HalconWindow);
            ClearObject();
            HideOutputlineColor = textBox3.Text;
            //这里要标注区域
            if (HideIntputArrayRows != null && HideIntputArrayRows.Length > 0)
            {
                HOperatorSet.SetColor(hw.HalconWindow, "blue");
                HOperatorSet.SetDraw(hw.HalconWindow, "fill");
                HOperatorSet.GenCircle(out ho_Biaozhu, HideIntputArrayRows[0], HideIntputArrayColumns[0], 20);
                HOperatorSet.DispObj(ho_Biaozhu, hw.HalconWindow);
                HOperatorSet.SetColor(hw.HalconWindow, HideOutputlineColor.ToLower());
                HOperatorSet.SetDraw(hw.HalconWindow, "margin");
            }

            //定义绘制线的开始坐标与结束坐标
            var lineTuple = new HTuple[] { 50, 100, 0, 100, 100 };
            //创建线的对象
            HDrawingObject lineObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE2, lineTuple);
            //设置线的颜色
            try
            {
                lineObj.SetDrawingObjectParams("color", HideOutputlineColor.ToLower());
            }
            catch
            {
                lineObj.SetDrawingObjectParams("color", "blue");
                throw new Exception("该线条不支持此颜色以为你选择默认颜色!");

            }
            //线段拖动触发事件注册
            lineObj.OnDrag(LineOnDrag);
            //线段长度修改触发事件注册
            lineObj.OnResize(LineOnResize);

            //永久保留该对象,不然会被垃圾回收机制回收
            line = new HDrawingObjectEx
            {
                HDrawingObject = lineObj,
                hTuples = lineTuple
            };
            //增加这个对象到指定Halcon窗口
            hw.HalconWindow.AttachDrawingObjectToWindow(lineObj);
            //输入
            HideIntputX = double.Parse(numericUpDown1.Value + "");
            HideIntputY = double.Parse(numericUpDown2.Value + "");
            HideIntputPhi = double.Parse(numericUpDown3.Value + "");
            HideIntputLength1 = double.Parse(numericUpDown7.Value + "");
            HideIntputLength2 = double.Parse(numericUpDown10.Value + "");

            this.numericUpDown1 = numericUpDown1;
            this.numericUpDown2 = numericUpDown2;
            this.numericUpDown3 = numericUpDown3;
            this.numericUpDown7 = numericUpDown7;
            this.numericUpDown10 = numericUpDown10;

            this.HideIntputThreshold = double.Parse(numericUpDown8.Value + "");
            this.HideIntputSigma = double.Parse(numericUpDown9.Value + "");
            this.HideIntputHeight = double.Parse(numericUpDown5.Value + "");
            this.HideIntputWidth = double.Parse(numericUpDown6.Value + "");
            this.HideOutputBorderColor = textBox1.Text;
            this.HideOutputCenterConlor = textBox2.Text;
            this.HideOutputlineColor = textBox3.Text;
            this.combox1 = combox1;

            //写入校验数据
            //创建目录检查
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string path1 = path + $"\\{form.Text}.txt";
            using (FileStream sw = new FileStream(path1, FileMode.Create, FileAccess.Write))
            {
                sw.Write(Encoding.UTF8.GetBytes(false.ToString()), 0, Encoding.UTF8.GetBytes(putCheck.ToString()).Length);
            }
        }
        /// <summary>
        /// 线段拖动触发事件
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        public void LineOnDrag(HDrawingObject drawid, HWindow window, string type)
        {
            UpdataDrawingObjecData(drawid);
        }
        /// <summary>
        /// 线段长度修改触发事件
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        public void LineOnResize(HDrawingObject drawid, HWindow window, string type)
        {
            UpdataDrawingObjecData(drawid);
        }
        /// <summary>
        /// 线段拖动修改长度方法
        /// </summary>
        /// <param name="drawid"></param>
        public void UpdataDrawingObjecData(HDrawingObject drawid)
        {
            if (numericUpDown1 != null)
            {
                //创建一个要得到对象属性的元组
                var attTuple = new HTuple("row", "column", "phi", "length1", "length2");
                //获取对应的属性
                var valueTuple = drawid.GetDrawingObjectParams(attTuple);
                //对对应的属性进行封装一下,后面好加入到集合中
                var attValues = new HTuple[] { valueTuple[0], valueTuple[1], valueTuple[2], valueTuple[3], valueTuple[4] };
                //更新数据
                line.hTuples = attValues;
                this.numericUpDown1.Value = decimal.Parse(valueTuple[1].D.ToString());
                this.numericUpDown2.Value = decimal.Parse(valueTuple[0].D.ToString());
                this.numericUpDown3.Value = decimal.Parse(valueTuple[3].D.ToString());
                //这里弧度会失败
                if (decimal.TryParse(valueTuple[4].D.ToString(), out decimal result1))
                {
                    this.numericUpDown7.Value = result1;
                }
                if (decimal.TryParse(valueTuple[2].D.ToString(), out decimal result2))
                {
                    this.numericUpDown10.Value = result2;
                }

                //写入校验数据
                //创建目录检查
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string path1 = path + $"\\{form.Text}.txt";
                using (FileStream sw = new FileStream(path1, FileMode.Create, FileAccess.Write))
                {
                    sw.Write(Encoding.UTF8.GetBytes(false.ToString()), 0, Encoding.UTF8.GetBytes(putCheck.ToString()).Length);
                }
            }
        }
        /// <summary>
        /// 清除这个线对象
        /// </summary>
        public void ClearObject()
        {
            if (lines != null && lines.Count > 0)
            {
                foreach (HDrawingObjectEx line in lines)
                {
                    var ho = line.HDrawingObject;
                    if (ho.ID == 0) { continue; }
                    hw.HalconWindow.DetachDrawingObjectFromWindow(ho);
                    ho.Dispose();
                }
            }
            if (line == null) return;
            var hobject = line.HDrawingObject;
            if (hobject.ID == 0) { return; }
            hw.HalconWindow.DetachDrawingObjectFromWindow(hobject);
            hobject.Dispose();
        }

        /// <summary>
        /// 放置卡尺
        /// </summary>
        private double skewing_X;  //偏移量X
        private double skewing_Y;//偏移量Y
        private double skewing_Angle;//偏移量角度

        public void SetCaliper()
        {
            if (HideIntputImage == null)
            {
                throw new Exception("当前图像为空");
            }

            HWindow hv_WindowHandle = hw.HalconWindow;
            //设置halcon图像模式
            HOperatorSet.SetSystem("clip_region", "false");
            HOperatorSet.GenRectangle2(out ho_ROI_1, double.Parse(form.numericUpDown2.Value.ToString()),
                double.Parse(form.numericUpDown1.Value.ToString()), double.Parse(form.numericUpDown10.Value.ToString()),
                double.Parse(form.numericUpDown3.Value.ToString()), double.Parse(form.numericUpDown7.Value.ToString()));
            //清除这个模型  
            ClearObject();
            HOperatorSet.SetColor(hv_WindowHandle, "green");
            HOperatorSet.SetDraw(hv_WindowHandle, "margin");
            HOperatorSet.DispObj(ho_ROI_1, hv_WindowHandle);
            //放置了卡尺为true
            putCheck = true;

            //计算偏移
            if (HideIntputArrayRows != null && HideIntputArrayRows.Length > 0)
            {
                firstModel_y = HideIntputArrayColumns[0];
                firstModel_x = HideIntputArrayRows[0];
                firstModel_phi = HideIntputArrayPhis[0];
                form.numericUpDown13.Value = decimal.Parse((HideIntputArrayPhis[0] - HideIntputPhi).ToString());
            }
            //写入校验数据
            //创建目录检查
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string path1 = path + $"\\{form.Text}.txt";
            using (FileStream sw = new FileStream(path1, FileMode.Create, FileAccess.Write))
            {
                sw.Write(Encoding.UTF8.GetBytes(putCheck.ToString() + $",{firstModel_x},{firstModel_y},{firstModel_phi}"), 0, Encoding.UTF8.GetBytes(putCheck.ToString() + $",{firstModel_x},{firstModel_y},{firstModel_phi}").Length);
            }
        }
        //读卡尺的
        public string[] strdb = null;
        /// <summary>
        /// 读取保存的卡尺数据信息文件
        /// </summary>
        public void LoadStrdb()
        {
            //读卡尺的
            strdb = new string[20];
            // 读取校验数据
            string path1 = path + $"\\{form.Text}.txt";
            byte[] data = new byte[1024 * 1024 * 2];
            try
            {
                using (FileStream sw = new FileStream(path1, FileMode.Open, FileAccess.Read))
                {
                    int index = sw.Read(data, 0, data.Length);
                    strdb = Encoding.UTF8.GetString(data, 0, index).Split(',');
                    bool.TryParse(strdb[0], out putCheck);
                }
            }
            catch
            {

            }
            // 读取校验数据
            if (line1Place == null)
            {
                List<double> line1Doubles = HelpJsons.Read<double>($@"Data\工程文件\{hwindowFlowNode.projectName}\{hwindowFlowNode.name}任务文件" + "\\测量线1基准数据" + "\\线段1点位数据.json");
                if (line1Doubles!=null)
                {
                    line1Place = new HTuple[] { line1Doubles[0], line1Doubles[1], line1Doubles[2], line1Doubles[3] };
                }
            }
            if (line1counterFrist == null)
            {
                line1counterFrist = HelpJsons.Read<double>($@"Data\工程文件\{hwindowFlowNode.projectName}\{hwindowFlowNode.name}任务文件" + "\\测量线1基准数据" + "\\线1线段模板基准数据.json");//线1线段模板基准数据
            }
            if (line2Place == null)
            {
                List<double> line2Doubles = HelpJsons.Read<double>($@"Data\工程文件\{hwindowFlowNode.projectName}\{hwindowFlowNode.name}任务文件" + "\\测量线2基准数据" + "\\线段2点位数据.json");
                if (line2Doubles!=null)
                {
                    line2Place = new HTuple[] { line2Doubles[0], line2Doubles[1], line2Doubles[2], line2Doubles[3] };
                }
               
            }
            if (line2counterFrist == null)
            {
                line2counterFrist = HelpJsons.Read<double>($@"Data\工程文件\{hwindowFlowNode.projectName}\{hwindowFlowNode.name}任务文件" + "\\测量线2基准数据" + "\\线2线段模板基准数据.json");
            }
        }
        /// <summary>
        /// 运行
        /// </summary>
        public void Run()
        {
            if (HideIntputImage == null)
            {
                throw new Exception("当前输入图像为空!");
            }
            //先清除原本的Dataview集合
            form.dataGridView1.Rows.Clear();
            if (form.comboBox2.Text=="开启")
            {
                ClearObject();
                strdb = new string[20];
                // 读取校验数据
                string path1 = path + $"\\{form.Text}.txt";
                byte[] data = new byte[1024 * 1024 * 2];
                try
                {
                    using (FileStream sw = new FileStream(path1, FileMode.Open, FileAccess.Read))
                    {
                        int index = sw.Read(data, 0, data.Length);
                        strdb = Encoding.UTF8.GetString(data, 0, index).Split(',');
                    }
                }
                catch
                {

                }
                //放置了卡尺
                if (putCheck)
                {
                    ///多目标测量
                    if (HideIntputArrayRows != null && HideIntputArrayRows.Length > 0)
                    {
                        //画多个卡尺工具跟随模板
                        DrawRectangle2s(HideIntputArrayColumns, HideIntputArrayRows, HideIntputArrayPhis, double.Parse((HideIntputArrayPhis[0] - HideIntputPhi).ToString()), strdb);
                    }
                    ///单次执行
                    else
                    {
                        HideOutputArrayDistances = new double[1];
                        try
                        {
                            HOperatorSet.SetColor(hw.HalconWindow, form.textBox1.Text.ToLower());
                        }
                        catch
                        {
                            HOperatorSet.SetColor(hw.HalconWindow, "blue");
                            throw new Exception("该线条不支持此颜色以为你选择默认颜色!");
                        }

                        HOperatorSet.GetImageSize(HideIntputImage, out HTuple hv_Width, out HTuple hv_Height);

                        HOperatorSet.GenMeasureRectangle2(double.Parse(form.numericUpDown2.Value.ToString()),
                            double.Parse(form.numericUpDown1.Value.ToString()), double.Parse(form.numericUpDown10.Value.ToString()),
                            double.Parse(form.numericUpDown3.Value.ToString()), double.Parse(form.numericUpDown7.Value.ToString()),
                            hv_Width, hv_Height, "bilinear", out HTuple hv_MeasureHandle);
                        ClearObject();
                        try
                        {
                            try
                            {
                                HOperatorSet.SetColor(hw.HalconWindow, form.textBox3.Text.ToLower());
                            }
                            catch
                            {
                                HOperatorSet.SetColor(hw.HalconWindow, "blue");
                                throw new Exception("该线条不支持此颜色以为你选择默认颜色!");
                            }

                            //自动模式
                            if (form.comboBox1.SelectedItem.ToString() == "auto")
                            {
                                //高斯 边缘阈值 浅色深色选择
                                HOperatorSet.MeasurePos(HideIntputImage, hv_MeasureHandle, this.HideIntputSigma, this.HideIntputThreshold, "positive",
                                    "all", out HTuple hv_RowEdge, out HTuple hv_ColumnEdge, out HTuple hv_Amplitude, out HTuple hv_Distance);
                                if (hv_Distance.Length == 0)
                                {
                                    HOperatorSet.MeasurePos(HideIntputImage, hv_MeasureHandle, this.HideIntputSigma, this.HideIntputThreshold, "negative",
                                    "all", out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude, out hv_Distance);
                                }

                                if (hv_Distance.Length == 0)
                                {

                                    HideOutputArrayDistances[0] = 0;
                                    form.dataGridView1.Rows.Add(0, 0);
                                    // throw new Exception("无法查找到边缘! Caliper-line-236!");                                                            
                                }
                                else
                                {
                                    HideOutputArrayDistances[0] = hv_Distance[0].D;
                                    form.dataGridView1.Rows.Add(0, hv_Distance[0].D);
                                    //画2个点之间的距离  distance:2点之间的距离
                                    HOperatorSet.GenRegionLine(out HObject regionLines, hv_RowEdge[0], hv_ColumnEdge[0], hv_RowEdge[1], hv_ColumnEdge[1]);
                                    //将线显示在图像上
                                    HOperatorSet.DispObj(regionLines, hw.HalconWindow);

                                }
                            }
                            else
                            {
                                //高斯 边缘阈值 浅色深色选择
                                HOperatorSet.MeasurePos(HideIntputImage, hv_MeasureHandle, this.HideIntputSigma, this.HideIntputThreshold, form.comboBox1.SelectedItem.ToString(),
                                    "all", out HTuple hv_RowEdge, out HTuple hv_ColumnEdge, out HTuple hv_Amplitude, out HTuple hv_Distance);

                                //距离
                                if (hv_Distance.Length == 0)
                                {
                                    HideOutputArrayDistances[0] = 0;
                                    form.dataGridView1.Rows.Add(0, 0);
                                    throw new Exception("无法查找到边缘! Caliper-line-236!");
                                }
                                else
                                {
                                    HideOutputArrayDistances[0] = hv_Distance[0].D;
                                    form.dataGridView1.Rows.Add(0, hv_Distance[0].D);
                                    //画2个点之间的距离  distance:2点之间的距离
                                    HOperatorSet.GenRegionLine(out HObject regionLines, hv_RowEdge[0], hv_ColumnEdge[0], hv_RowEdge[1], hv_ColumnEdge[1]);

                                    //将线显示在图像上
                                    HOperatorSet.DispObj(regionLines, hw.HalconWindow);
                                }
                                HideOutputArrayDistances[0] = hv_Distance;
                            }
                        }
                        catch (Exception ex)
                        {
                            string str = ex.Message.Contains("无法查找到边缘") ? ex.Message : "精准测量工具运行失败 Caliper-line-241!";
                            throw new Exception(str);
                        }
                    }
                }
                //没有放置
                else
                {
                    ClearObject();
                    throw new Exception("请先放置卡尺!");
                }
            }else if (form.comboBox3.Text=="开启")
            {
                Line1ToLine2();
            }
        }

        /// <summary>
        /// shuzhi移动
        /// </summary>
        public void Move_X(double x1, double y1, double phi, double length1, double length2)
        {
            //ClearObject();
            //HideIntputX = x1;
            //HideIntputY = y1;
            //HideIntputPhi = phi;
            //HideIntputLength1 = length1;
            //HideIntputLength2 = length2;
            ////定义绘制线的开始坐标与结束坐标
            //var lineTuple = new HTuple[] { y1, x1, phi, length1 , length2};
            ////创建线的对象
            //var lineObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE2, lineTuple);
            ////线段拖动触发事件注册
            //lineObj.OnDrag(LineOnDrag);
            ////线段长度修改触发事件注册
            //lineObj.OnResize(LineOnResize);

            ////设置线的颜色
            //try
            //{
            //    lineObj.SetDrawingObjectParams("color", HideOutputlineColor.ToLower());
            //}
            //catch
            //{
            //    (FormDb.Db["Form1"] as Form1).textBox1.AppendText("该线条不支持此颜色以为你选择默认颜色!\r\n");
            //    lineObj.SetDrawingObjectParams("color", "blue");
            //}

            ////永久保留该对象,不然会被垃圾回收机制回收
            //line = new HDrawingObjectEx
            //{
            //    HDrawingObject = lineObj,
            //    hTuples = lineTuple
            //};
            ////增加这个对象到指定Halcon窗口
            //hw.HalconWindow.AttachDrawingObjectToWindow(lineObj);
        }
        /// <summary>
        /// 修改平滑与对比度
        /// </summary>
        /// <param name="sigam"></param>
        /// <param name="threshold"></param>
        public void Updata_SigamThreshold(double sigam, double threshold)
        {
            this.HideIntputSigma = sigam;
            this.HideIntputThreshold = threshold;
        }

        /// <summary>
        /// 工具的单次运行
        /// </summary>
        public void Tool_Run()
        {

            if (HideIntputImage == null)
            {
                throw new Exception("当前图像为空");
            }
            hw.HalconWindow.ClearWindow();
            HOperatorSet.DispObj(HideIntputImage, hw.HalconWindow);
            Run();
        }

        /// <summary>z
        /// 创建多个旋转矩形,根据模板的识别出的数据决定
        /// </summary>
        public void DrawRectangle2s(double[] IntputColums, double[] IntputRows, double[] IntputAngles, double PianYiPhi, string[] strdb)
        {

            //声明数组长度
            int Dbcount = 0;
            HideOutputArrayDistances = new double[IntputColums.Length];
            HideOutputArrayColumns = new double[IntputColums.Length];
            HideOutputArrayRows = new double[IntputColums.Length];
            HObject[] ecordRegion = new HObject[IntputColums.Length];
            HWindow hwindow = hw.HalconWindow;
            lines.Clear();
            //将存储线段的二维数组赋值
            linesList = new double[IntputColums.Length, 4];
            for (int i = 0; i < IntputColums.Length; i++)
            {
                //创建线的对象
                HOperatorSet.GenRectangle2(out HObject hLine, double.Parse(form.numericUpDown2.Value.ToString()), double.Parse(form.numericUpDown1.Value.ToString()), double.Parse(form.numericUpDown10.Value.ToString()),
                    double.Parse(form.numericUpDown3.Value.ToString()), double.Parse(form.numericUpDown7.Value.ToString()));

                HObject ho_RegionAffineTrans = UtiyHalcon.MatrixCalculate(hLine, IntputRows[i], IntputColums[i], IntputAngles[i], double.Parse(strdb[1]), double.Parse(strdb[2]), double.Parse(strdb[3]));

                //获取矩形的位置信息
                HOperatorSet.SmallestRectangle2(ho_RegionAffineTrans, out HTuple row, out HTuple col, out HTuple phi, out HTuple length1, out HTuple length2);

                if (row.D == 0)
                {
                    HideOutputArrayDistances[Dbcount] = 0;
                    form.dataGridView1.Rows.Add(Dbcount, 0);
                    Dbcount++;
                    ecordRegion[i] = hLine;
                    continue;
                }
                //显示出每个卡尺
                try
                {
                    HOperatorSet.SetColor(hw.HalconWindow, form.textBox3.Text.ToLower());
                }
                catch
                {
                    HOperatorSet.SetColor(hw.HalconWindow, "blue");
                    form.textBox1.Text = "blue";
                    //throw new Exception("该线条不支持此颜色以为你选择默认颜色!");
                }
                HOperatorSet.SetDraw(hw.HalconWindow, "margin");
                HOperatorSet.DispRectangle2(hw.HalconWindow, row, col, phi, length1, length2);
                HOperatorSet.SetDraw(hw.HalconWindow, "fill");

                //开始计算距离
                HOperatorSet.GetImageSize(HideIntputImage, out HTuple hv_Width, out HTuple hv_Height);



                HOperatorSet.GenMeasureRectangle2(row, col, phi, length1,
                    length2, hv_Width, hv_Height, "bilinear", out HTuple hv_MeasureHandle);
                ClearObject();
                try
                {
                    //自动模式
                    if (form.comboBox1.SelectedItem.ToString() == "auto")
                    {
                        //高斯 边缘阈值 浅色深色选择
                        HOperatorSet.MeasurePos(HideIntputImage, hv_MeasureHandle, this.HideIntputSigma, this.HideIntputThreshold, "positive",
                            "all", out HTuple hv_RowEdge, out HTuple hv_ColumnEdge, out HTuple hv_Amplitude, out HTuple hv_Distance);
                        if (hv_Distance.Length == 0)
                        {
                            HOperatorSet.MeasurePos(HideIntputImage, hv_MeasureHandle, this.HideIntputSigma, this.HideIntputThreshold, "negative",
                            "all", out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude, out hv_Distance);

                        }

                        try
                        {
                            HOperatorSet.SetColor(hw.HalconWindow, form.textBox1.Text.ToLower());
                        }
                        catch
                        {
                            HOperatorSet.SetColor(hw.HalconWindow, "blue");
                            form.textBox1.Text = "blue";
                            // throw new Exception("该线条不支持此颜色以为你选择默认颜色!");
                        }
                        if (hv_Distance.Length == 0)
                        {
                            HideOutputArrayDistances[Dbcount] = 0;
                            form.dataGridView1.Rows.Add(Dbcount, 0);
                            ecordRegion[i] = hLine;
                            Dbcount++;
                            continue;
                            //throw new Exception("无法查找到边缘! Caliper-line-236!");
                        }
                        else
                        {
                            //画2个点之间的距离  distance:2点之间的距离
                            HOperatorSet.GenRegionLine(out HObject regionLines, hv_RowEdge[0], hv_ColumnEdge[0], hv_RowEdge[1], hv_ColumnEdge[1]);

                            HOperatorSet.AreaCenter(regionLines, out HTuple areaLine, out HTuple rowLine, out HTuple columnLine);
                            ecordRegion[i] = regionLines;
                            HideOutputArrayColumns[i] = columnLine;
                            HideOutputArrayRows[i] = rowLine;
                            //将线显示在图像上
                            HOperatorSet.DispObj(regionLines, hw.HalconWindow);
                            ///这里只要第0的下标的距离数据,因为卡尺会卡住多个边,不能精确到那条数据
                            HideOutputArrayDistances[Dbcount] = hv_Distance[0].D;
                            form.dataGridView1.Rows.Add(Dbcount, hv_Distance[0].D);
                            Dbcount++;
                        }
                        //HOperatorSet.DispCross(hv_WindowHandle, hv_RowEdge, hv_ColumnEdge, 10, (new HTuple(90)).TupleRad());
                    }
                    else
                    {
                        //高斯 边缘阈值 浅色深色选择
                        HOperatorSet.MeasurePos(HideIntputImage, hv_MeasureHandle, this.HideIntputSigma, this.HideIntputThreshold, form.comboBox1.SelectedItem.ToString(),
                            "all", out HTuple hv_RowEdge, out HTuple hv_ColumnEdge, out HTuple hv_Amplitude, out HTuple hv_Distance);
                        //距离
                        if (hv_Distance.Length == 0)
                        {
                            ecordRegion[i] = hLine;
                            HideOutputArrayDistances[Dbcount] = 0;
                            form.dataGridView1.Rows.Add(Dbcount, 0);
                            Dbcount++;
                        }
                        else
                        {
                            try
                            {
                                HOperatorSet.SetColor(hw.HalconWindow, form.textBox1.Text.ToLower());
                            }
                            catch
                            {
                                HOperatorSet.SetColor(hw.HalconWindow, "blue");
                                form.textBox1.Text = "blue";
                                //throw new Exception("该线条不支持此颜色以为你选择默认颜色!");
                            }
                            HOperatorSet.GenContourPolygonXld(out HObject regionLines1,new double[] { hv_RowEdge[0].D, hv_RowEdge[1].D },new double[] { hv_ColumnEdge[0].D, hv_ColumnEdge[1].D });
                            //画2个点之间的距离  distance:2点之间的距离
                            HOperatorSet.GenRegionLine(out HObject regionLines, hv_RowEdge[0], hv_ColumnEdge[0], hv_RowEdge[1], hv_ColumnEdge[1]);
                            //将线段的2个点信息存入二维数组当中
                            linesList[i, 0] = hv_RowEdge[0];
                            linesList[i, 1] = hv_ColumnEdge[0];
                            linesList[i, 2] = hv_RowEdge[1];
                            linesList[i, 3] = hv_ColumnEdge[1];
                            ecordRegion[i] = regionLines;

                            HOperatorSet.AreaCenter(regionLines, out HTuple areaLine, out HTuple rowLine, out HTuple columnLine);
                            HideOutputArrayColumns[i] = columnLine;
                            HideOutputArrayRows[i] = rowLine;
                            //将线显示在图像上
                            HOperatorSet.DispObj(regionLines1, hw.HalconWindow);
                            HideOutputArrayDistances[Dbcount] = hv_Distance[0].D;
                            form.dataGridView1.Rows.Add(Dbcount, hv_Distance[0].D);
                            Dbcount++;
                        }
                    }
                }
                catch (Exception ex)
                {
                    string str = ex.Message.Contains("无法查找到边缘") ? ex.Message : "精准测量工具运行失败 Caliper-line-241!";
                    throw new Exception(str);

                }
            }

        }
        public void HardRun(HSmartWindowControl hSmartWindowControl,HObject HideIntputImage, double[] IntputRows,double[] IntputAngles, double[] IntputColums, PreciseCaliper form,string[] strdb,double HideIntputSigma, double HideIntputThreshold, ref double[,] linesList,ref double[] HideOutputArrayDistances,ref double[] HideOutputArrayRows,ref double[] HideOutputArrayColumns,HTuple[] line1Place,HTuple[] line2Place, List<double> line1counterFrist,List<double> line2counterFrist)
        {
            //声明数组长度
            HideOutputArrayDistances = new double[IntputColums.Length];
            HideOutputArrayColumns = new double[IntputColums.Length];
            HideOutputArrayRows = new double[IntputColums.Length];
            if (form.comboBox2.Text == "开启")
            {
                
                //将存储线段的二维数组赋值
                linesList = new double[IntputColums.Length, 4];
                for (int i = 0; i < IntputColums.Length; i++)
                {
                    //创建线的对象
                    HOperatorSet.GenRectangle2(out HObject hLine, double.Parse(form.numericUpDown2.Value.ToString()), double.Parse(form.numericUpDown1.Value.ToString()), double.Parse(form.numericUpDown10.Value.ToString()),
                        double.Parse(form.numericUpDown3.Value.ToString()), double.Parse(form.numericUpDown7.Value.ToString()));

                    HObject ho_RegionAffineTrans = UtiyHalcon.MatrixCalculate(hLine, IntputRows[i], IntputColums[i], IntputAngles[i], double.Parse(strdb[1]), double.Parse(strdb[2]), double.Parse(strdb[3]));

                    //获取矩形的位置信息
                    HOperatorSet.SmallestRectangle2(ho_RegionAffineTrans, out HTuple row, out HTuple col, out HTuple phi, out HTuple length1, out HTuple length2);

                    if (row.D == 0)
                    {
                        HideOutputArrayDistances[i] = 0;
                        continue;
                    }
                    //显示出每个卡尺
                    try
                    {
                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, form.textBox3.Text.ToLower());
                    }
                    catch
                    {
                        HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "blue");
                        form.textBox1.Text = "blue";
                        //throw new Exception("该线条不支持此颜色以为你选择默认颜色!");
                    }
                    HOperatorSet.SetDraw(hSmartWindowControl.HalconWindow, "margin");
                    if (form.comboBox4.Text == "开启")
                    {
                        HOperatorSet.DispRectangle2(hSmartWindowControl.HalconWindow, row, col, phi, length1, length2);
                    }

                    //开始计算距离
                    HOperatorSet.GetImageSize(HideIntputImage, out HTuple hv_Width, out HTuple hv_Height);


                    HOperatorSet.GenMeasureRectangle2(row, col, phi, length1,
                        length2, hv_Width, hv_Height, "bilinear", out HTuple hv_MeasureHandle);


                    //ClearObject();
                    try
                    {
                        //自动模式
                        if (form.comboBox1.SelectedItem.ToString() == "auto")
                        {
                            //高斯 边缘阈值 浅色深色选择
                            HOperatorSet.MeasurePos(HideIntputImage, hv_MeasureHandle, HideIntputSigma, HideIntputThreshold, "positive",
                                "all", out HTuple hv_RowEdge, out HTuple hv_ColumnEdge, out HTuple hv_Amplitude, out HTuple hv_Distance);
                            if (hv_Distance.Length == 0)
                            {
                                HOperatorSet.MeasurePos(HideIntputImage, hv_MeasureHandle, HideIntputSigma, HideIntputThreshold, "negative",
                                "all", out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude, out hv_Distance);

                            }

                            try
                            {
                                HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, form.textBox1.Text.ToLower());
                            }
                            catch
                            {
                                HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "blue");
                                form.textBox1.Text = "blue";
                                // throw new Exception("该线条不支持此颜色以为你选择默认颜色!");
                            }
                            if (hv_Distance.Length == 0)
                            {
                                HideOutputArrayDistances[i] = 0;
                                continue;
                                //throw new Exception("无法查找到边缘! Caliper-line-236!");
                            }
                            else
                            {
                                //画2个点之间的距离  distance:2点之间的距离
                                HOperatorSet.GenRegionLine(out HObject regionLines, hv_RowEdge[0], hv_ColumnEdge[0], hv_RowEdge[1], hv_ColumnEdge[1]);

                                HOperatorSet.AreaCenter(regionLines, out HTuple areaLine, out HTuple rowLine, out HTuple columnLine);
                                HideOutputArrayColumns[i] = columnLine;
                                HideOutputArrayRows[i] = rowLine;
                                //将线显示在图像上
                                HOperatorSet.DispObj(regionLines, hSmartWindowControl.HalconWindow);
                                ///这里只要第0的下标的距离数据,因为卡尺会卡住多个边,不能精确到那条数据
                                HideOutputArrayDistances[i] = hv_Distance[0].D;
                            }
                        }
                        else
                        {
                            //高斯 边缘阈值 浅色深色选择
                            HOperatorSet.MeasurePos(HideIntputImage, hv_MeasureHandle, HideIntputSigma, HideIntputThreshold, form.comboBox1.SelectedItem.ToString(),
                                "all", out HTuple hv_RowEdge, out HTuple hv_ColumnEdge, out HTuple hv_Amplitude, out HTuple hv_Distance);
                            //距离
                            if (hv_Distance.Length == 0)
                            {
                                HideOutputArrayDistances[i] = 0;
                            }
                            else
                            {
                                try
                                {
                                    HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, form.textBox1.Text.ToLower());
                                }
                                catch
                                {
                                    HOperatorSet.SetColor(hSmartWindowControl.HalconWindow, "blue");
                                }

                                //画2个点之间的距离  distance:2点之间的距离
                                HOperatorSet.GenRegionLine(out HObject regionLines, hv_RowEdge[0], hv_ColumnEdge[0], hv_RowEdge[1], hv_ColumnEdge[1]);
                                //将线段的2个点信息存入二维数组当中
                                linesList[i, 0] = hv_RowEdge[0];
                                linesList[i, 1] = hv_ColumnEdge[0];
                                linesList[i, 2] = hv_RowEdge[1];
                                linesList[i, 3] = hv_ColumnEdge[1];

                                HOperatorSet.AreaCenter(regionLines, out HTuple areaLine, out HTuple rowLine, out HTuple columnLine);
                                HideOutputArrayColumns[i] = columnLine;
                                HideOutputArrayRows[i] = rowLine;
                                //将线显示在图像上
                                HOperatorSet.DispObj(regionLines, hSmartWindowControl.HalconWindow);
                                HideOutputArrayDistances[i] = hv_Distance[0].D;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        string str = ex.Message.Contains("无法查找到边缘") ? ex.Message : "精准测量工具运行失败 Caliper-line-241!";
                        throw new Exception(str);

                    }
                }
            }
            else if (form.comboBox3.Text =="开启")
            {
                //这里要运行线与线的卡尺测量了
                if (IntputColums != null)
                {
                    try
                    {
                        linesList = new double[IntputColums.Length, 4];
                        HideOutputArrayRows = new double[IntputColums.Length];
                        HideOutputArrayColumns = new double[IntputColums.Length];
                        for (int i = 0; i < IntputColums.Length; i++)
                        {
                            //创建线1的对象
                            HOperatorSet.GenRegionLine(out HObject hLine1, double.Parse(form.caliper1Row1.Value + ""), double.Parse(form.caliper1Col1.Value + ""), double.Parse(form.caliper1Row2.Value + ""), double.Parse(form.caliper1Col2.Value + ""));
                            //生成第一条跟随的线
                            HObject ho_RegionAffineTrans1 = UtiyHalcon.MatrixCalculate(hLine1, IntputRows[i], IntputColums[i], IntputAngles[i], line1counterFrist[0], line1counterFrist[1], line1counterFrist[2]);
                            HOperatorSet.GetRegionPoints(ho_RegionAffineTrans1, out HTuple hv_Rows1, out HTuple hv_Columns1);
                            if (hv_Rows1.Length == 0)
                            {
                                HideOutputArrayDistances[i] = 0;
                                continue;
                            }
                            //创建计量模型，进行找线
                            HOperatorSet.CreateMetrologyModel(out HTuple hv_MetrologyHandle);
                            //增加一个矩形的计量模型
                            double[] hTuple = new double[] { hv_Rows1[0], hv_Columns1[0], hv_Rows1[hv_Rows1.Length - 1], hv_Columns1[hv_Columns1.Length - 1] };

                            //HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", hTuple, (int)pmaForm.numericUpDown17.Value, (int)pmaForm.numericUpDown16.Value, double.Parse(pmaForm.numericUpDown2.Value + ""), double.Parse(pmaForm.numericUpDown3.Value + ""), new HTuple(), new HTuple(), out HTuple hv_Index);

                            HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", hTuple, (int)form.line1HeightBox.Value, (int)form.line1Width.Value, double.Parse(form.line1muscle.Value + ""), double.Parse(form.line1contrast.Value + ""), new HTuple(), new HTuple(), out HTuple hv_Index);
                            string polarity = "";
                            if (form.line1Combox.SelectedItem.ToString() == "全部")
                            {
                                polarity = "all";
                            }
                            if (form.line1Combox.SelectedItem.ToString() == "正极性")
                            {
                                polarity = "negative";
                            }
                            if (form.line1Combox.SelectedItem.ToString() == "负极性")
                            {
                                polarity = "positive";
                            }
                            if (form.line1Combox.SelectedItem.ToString() == "仅有")
                            {
                                polarity = "uniform";
                            }
                            //设置极性
                            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_transition", polarity);
                            //卡尺的数量
                            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "num_measures", (int)form.line1NumberBox.Value);
                            //卡尺卡的数量 接受的结果数
                            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "num_instances", 1);
                            //bilinear
                            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_interpolation", "bicubic");
                            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_select", "all");
                            //最小分数
                            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "min_score", double.Parse(form.line1small.Value.ToString()));
                            //开始抓
                            HOperatorSet.ApplyMetrologyModel(HideIntputImage, hv_MetrologyHandle);
                            HOperatorSet.GetMetrologyObjectMeasures(out HObject ho_Contours, hv_MetrologyHandle, "all", "all",
                                        out HTuple hv_Row, out HTuple hv_Column);

                            HOperatorSet.GenRegionLine(out HObject regionLine1, hv_Row[0], hv_Column[0], hv_Row[hv_Row.Length - 1], hv_Column[hv_Column.Length - 1]);

                            HOperatorSet.GenContourPolygonXld(out HObject regionLines1, new double[] { hv_Row[0].D, hv_Row[1].D }, new double[] { hv_Column[0].D, hv_Column[1].D });
                            if (form.comboBox4.Text == "开启")
                            {
                                HOperatorSet.DispObj(regionLines1, hSmartWindowControl.HalconWindow);
                            }
                            //创建线2的对象
                            //创建线1的对象
                            HOperatorSet.GenRegionLine(out HObject hLine2, double.Parse(form.caliper2Row1.Value + ""), double.Parse(form.caliper2Col1.Value + ""), double.Parse(form.caliper2Row2.Value + ""), double.Parse(form.caliper2Col2.Value + ""));
                            //生成第一条跟随的线
                            HObject ho_RegionAffineTrans2 = UtiyHalcon.MatrixCalculate(hLine2, IntputRows[i], IntputColums[i], IntputAngles[i], line2counterFrist[0], line2counterFrist[1], line2counterFrist[2]);
                            HOperatorSet.GetRegionPoints(ho_RegionAffineTrans2, out HTuple hv_Rows2, out HTuple hv_Columns2);
                            if (hv_Rows2.Length == 0)
                            {
                                HideOutputArrayDistances[i] = 0;
                                continue;
                            }
                            //创建计量模型，进行找线
                            HOperatorSet.CreateMetrologyModel(out HTuple hv_MetrologyHandle2);
                            //增加一个矩形的计量模型
                            double[] hTuple2 = new double[] { hv_Rows2[0], hv_Columns2[0], hv_Rows2[hv_Rows2.Length - 1], hv_Columns2[hv_Columns2.Length - 1] };

                            HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle2, "line", hTuple2, (int)form.line2HeightBox.Value, (int)form.line2Width.Value, double.Parse(form.line2muscle.Value + ""), double.Parse(form.line2contrast.Value + ""), new HTuple(), new HTuple(), out HTuple hv_Index2);
                            string polarity2 = "";
                            if (form.line2Combox.SelectedItem.ToString() == "全部")
                            {
                                polarity2 = "all";
                            }
                            if (form.line2Combox.SelectedItem.ToString() == "正极性")
                            {
                                polarity2 = "negative";
                            }
                            if (form.line2Combox.SelectedItem.ToString() == "负极性")
                            {
                                polarity2 = "positive";
                            }
                            if (form.line2Combox.SelectedItem.ToString() == "仅有")
                            {
                                polarity2 = "uniform";
                            }
                            //设置极性
                            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle2, "all", "measure_transition", polarity2);
                            //卡尺的数量
                            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle2, "all", "num_measures", (int)form.line2NumberBox.Value);
                            //卡尺卡的数量 接受的结果数
                            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle2, "all", "num_instances", 1);
                            //bilinear
                            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle2, "all", "measure_interpolation", "bicubic");
                            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle2, "all", "measure_select", "all");
                            //最小分数
                            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle2, "all", "min_score", double.Parse(form.line2small.Value.ToString()));
                            //开始抓
                            HOperatorSet.ApplyMetrologyModel(HideIntputImage, hv_MetrologyHandle2);
                            HOperatorSet.GetMetrologyObjectMeasures(out HObject ho_Contours2, hv_MetrologyHandle2, "all", "all",
                                        out HTuple hv_Row2, out HTuple hv_Column2);
                            //生产一条线
                            HOperatorSet.GenRegionLine(out HObject regionLine2, hv_Row2[0], hv_Column2[0], hv_Row2[hv_Row2.Length - 1], hv_Column2[hv_Column2.Length - 1]);
                            HOperatorSet.GenContourPolygonXld(out HObject regionLines2, new double[] { hv_Row2[0].D, hv_Row2[1].D }, new double[] { hv_Column2[0].D, hv_Column2[1].D });
                            if (form.comboBox4.Text == "开启")
                            {
                                HOperatorSet.DispObj(regionLines2, hSmartWindowControl.HalconWindow);
                            }
                            //计算线到线之间的距离  distance_ss    distanceMin:2条线段最小距离       distanceMax:2条线段最大距离
                            HOperatorSet.DistanceSs(hv_Row[0], hv_Column[0], hv_Row[hv_Row.Length - 1], hv_Column[hv_Column.Length - 1], hv_Row2[0], hv_Column2[0], hv_Row2[hv_Row2.Length - 1], hv_Column2[hv_Column2.Length - 1], out HTuple distanceMin, out HTuple distanceMax);
                            HideOutputArrayDistances[i] = distanceMin.D;
                            //将数据给间距数组
                            linesList[i, 0] = hv_Row[0];
                            linesList[i, 1] = hv_Column[0];
                            linesList[i, 2] = hv_Row2[0];
                            linesList[i, 3] = hv_Column2[0];
                            //生成一条测试的线段
                            HOperatorSet.AreaCenter(regionLine1, out HTuple area1, out HTuple rowLine1, out HTuple columnLine1);
                            HOperatorSet.AreaCenter(regionLine2, out HTuple area2, out HTuple rowLine2, out HTuple columnLine2);
                            HOperatorSet.GenRegionLine(out HObject hObjectLine, rowLine1, columnLine1, rowLine2, columnLine2);
                            HOperatorSet.DispObj(hObjectLine, hSmartWindowControl.HalconWindow);
                            //显示文本
                            HOperatorSet.AreaCenter(hObjectLine, out HTuple area3, out HTuple rowLine3, out HTuple columnLine3);
                            HideOutputArrayRows = rowLine3;
                            HideOutputArrayColumns = columnLine3;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("线段1抓取失败" + ex.Message);
                        
                    }
                }
            }
        }
        //线段1的对象
        public HTuple[] line1Place = new HTuple[] { 100, 0, 100, 200 };//线段1位置
        public HDrawingObject line1Object;//线段1的对象
        //线段2的对象
        public HTuple[] line2Place = new HTuple[] { 200, 0, 200, 200 };//线段2位置
        public HDrawingObject line2Object;//线段2的对象
        /// <summary>
        /// 线到线功能的使用开始
        /// </summary>
        public void DrawLineToLine()
        {
            HOperatorSet.ClearWindow(hw.HalconWindow);
            HOperatorSet.DispObj(HideIntputImage, hw.HalconWindow);

            line1Place = new HTuple[] { double.Parse(form.caliper1Row1.Value + ""), double.Parse(form.caliper1Col1.Value + ""), double.Parse(form.caliper1Row2.Value + ""), double.Parse(form.caliper1Col2.Value + "") };
            line2Place = new HTuple[] { double.Parse(form.caliper2Row1.Value + ""), double.Parse(form.caliper2Col1.Value + ""), double.Parse(form.caliper2Row2.Value + ""), double.Parse(form.caliper2Col2.Value + "") };

            //线段1的对象生成
            line1Object = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.LINE, line1Place);
            //线段2的对象生成
            line2Object = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.LINE, line2Place);

            //生成一个目标位置
            HOperatorSet.SetDraw(hwindowFlowNode.hSmartWindowControl1.HalconWindow,"fill");
            HOperatorSet.GenCircle(out HObject circle, HideIntputArrayRows[0], HideIntputArrayColumns[0], 20);
            HOperatorSet.SetColor(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "blue");
            HOperatorSet.DispObj(circle, hwindowFlowNode.hSmartWindowControl1.HalconWindow);
            HOperatorSet.SetDraw(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "margin");
            HOperatorSet.SetColor(hwindowFlowNode.hSmartWindowControl1.HalconWindow, "green");
            //为线段1注册事件
            //线段拖动触发事件注册
            line1Object.OnDrag(LineOnDrag1);
            //线段长度修改触发事件注册
            line1Object.OnResize(LineOnResize1);

            //为线段2注册事件
            //线段拖动触发事件注册
            line2Object.OnDrag(LineOnDrag2);
            //线段长度修改触发事件注册
            line2Object.OnResize(LineOnResize2);
            line1Object.SetDrawingObjectParams("color", "green");
            line2Object.SetDrawingObjectParams("color", "blue");
            //将2个线段显示在窗口上
            hw.HalconWindow.AttachDrawingObjectToWindow(line2Object);
            hw.HalconWindow.AttachDrawingObjectToWindow(line1Object);
        }
        /// <summary>
        /// 1线段长度修改触发事件
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void LineOnResize1(HDrawingObject drawid, HWindow window, string type)
        {
            UpdataDrawingObjecDatLine1(drawid);
        }
        /// <summary>
        /// 1线段拖动触发事件
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void LineOnDrag1(HDrawingObject drawid, HWindow window, string type)
        {
            UpdataDrawingObjecDatLine1(drawid);
        }
        /// <summary>
        /// 1线段长度修改触发事件
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void LineOnResize2(HDrawingObject drawid, HWindow window, string type)
        {
            UpdataDrawingObjecDatLine2(drawid);
        }
        /// <summary>
        /// 1线段拖动触发事件
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void LineOnDrag2(HDrawingObject drawid, HWindow window, string type)
        {
            UpdataDrawingObjecDatLine2(drawid);
        }
        //线1的第一次模板的数据
        public List<double> line1counterFrist;
        /// <summary>
        /// 线段1线段修改具体实现
        /// </summary>
        private void UpdataDrawingObjecDatLine1(HDrawingObject drawid)
        {
            try
            {
                //创建一个要得到对象属性的元组
                var attTuple = new HTuple("row1", "column1", "row2", "column2");
                //获取对应的属性
                var valueTuple = drawid.GetDrawingObjectParams(attTuple);
                //对对应的属性进行封装一下,后面好加入到集合中
                line1Place = new HTuple[] { valueTuple[0], valueTuple[1], valueTuple[2], valueTuple[3] };

                form.caliper1Row1.Value = decimal.Parse(valueTuple[0].D+"");
                form.caliper1Col1.Value = decimal.Parse(valueTuple[1].D + "");
                form.caliper1Row2.Value = decimal.Parse(valueTuple[2].D + "");
                form.caliper1Col2.Value = decimal.Parse(valueTuple[3].D + "");

                line1counterFrist = new List<double>();
                List<double> line1Doubles = new List<double>();
                //记录第一次模板的位置
                line1counterFrist.Add(HideIntputArrayRows[0]);
                line1counterFrist.Add(HideIntputArrayColumns[0]);
                line1counterFrist.Add(HideIntputArrayPhis[0]);
                //保存线段数据
                line1Doubles.Add(valueTuple[0]);
                line1Doubles.Add(valueTuple[1]);
                line1Doubles.Add(valueTuple[2]);
                line1Doubles.Add(valueTuple[3]);
                //将这个基准数据进行保存
                string path = $@"Data\工程文件\{hwindowFlowNode.projectName}\{hwindowFlowNode.name}任务文件\测量线1基准数据";
                if (Directory.Exists(path))
                {
                    Directory.Delete(path, true);
                }
                Directory.CreateDirectory(path);
                HelpJsons.Write<double>(line1counterFrist, path + "\\线1线段模板基准数据.json");
                HelpJsons.Write<double>(line1Doubles, path + "\\线段1点位数据.json");
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText("线1移动时失败" + ":" + ex.Message + "\r\n");
            }
        }
        //线2的第一次模板的数据
        public List<double> line2counterFrist;
        /// <summary>
        ///  线段2线段修改具体实现
        /// </summary>
        private void UpdataDrawingObjecDatLine2(HDrawingObject drawid)
        {
            try
            {
                //创建一个要得到对象属性的元组
                var attTuple = new HTuple("row1", "column1", "row2", "column2");
                //获取对应的属性
                var valueTuple = drawid.GetDrawingObjectParams(attTuple);
                //对对应的属性进行封装一下,后面好加入到集合中
                line2Place = new HTuple[] { valueTuple[0], valueTuple[1], valueTuple[2], valueTuple[3] };
                line2counterFrist = new List<double>();
                List<double> line1Doubles = new List<double>();
                //记录第一次模板的位置
                line2counterFrist.Add(HideIntputArrayRows[0]);
                line2counterFrist.Add(HideIntputArrayColumns[0]);
                line2counterFrist.Add(HideIntputArrayPhis[0]);

                form.caliper2Row1.Value = decimal.Parse(valueTuple[0].D + "");
                form.caliper2Col1.Value = decimal.Parse(valueTuple[1].D + "");
                form.caliper2Row2.Value = decimal.Parse(valueTuple[2].D + "");
                form.caliper2Col2.Value = decimal.Parse(valueTuple[3].D + "");

                //保存线段数据
                line1Doubles.Add(valueTuple[0]);
                line1Doubles.Add(valueTuple[1]);
                line1Doubles.Add(valueTuple[2]);
                line1Doubles.Add(valueTuple[3]);
                //将这个基准数据进行保存
                string path = $@"Data\工程文件\{hwindowFlowNode.projectName}\{hwindowFlowNode.name}任务文件\测量线2基准数据";
                if (Directory.Exists(path))
                {
                    Directory.Delete(path, true);
                }
                Directory.CreateDirectory(path);
                HelpJsons.Write<double>(line2counterFrist, path + "\\线2线段模板基准数据.json");
                HelpJsons.Write<double>(line1Doubles, path + "\\线段2点位数据.json");
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText("线2移动时失败" + ":" + ex.Message + "\r\n");
            }
        }
        /// <summary>
        /// 线与线之间的距离
        /// </summary>
        /// <exception cref="Exception"></exception>
        private void Line1ToLine2()
        {
            //这里要运行线与线的卡尺测量了
            if (HideIntputArrayColumns != null)
            {
                //卡尺卡的距离
                HideOutputArrayDistances=new double[HideIntputArrayColumns.Length];
                try
                {
                    form.dataGridView1.Rows.Clear();
                    linesList = new double[HideIntputArrayColumns.Length,4];
                    HideOutputArrayRows=new double[HideIntputArrayColumns.Length];
                    HideOutputArrayColumns = new double[HideIntputArrayColumns.Length];
                    for (int i = 0; i < HideIntputArrayColumns.Length; i++)
                    {
                        //创建线1的对象
                        HOperatorSet.GenRegionLine(out HObject hLine1, double.Parse(form.caliper1Row1.Value + ""), double.Parse(form.caliper1Col1.Value + ""), double.Parse(form.caliper1Row2.Value + ""), double.Parse(form.caliper1Col2.Value + ""));
                        //生成第一条跟随的线
                        HObject ho_RegionAffineTrans1 = UtiyHalcon.MatrixCalculate(hLine1, HideIntputArrayRows[i], HideIntputArrayColumns[i], HideIntputArrayPhis[i], line1counterFrist[0], line1counterFrist[1], line1counterFrist[2]);
                        HOperatorSet.GetRegionPoints(ho_RegionAffineTrans1, out HTuple hv_Rows1, out HTuple hv_Columns1);
                        if (hv_Rows1.Length == 0)
                        {

                            HideOutputArrayDistances[i] = 0;
                            continue;
                        }
                        //创建计量模型，进行找线
                        HOperatorSet.CreateMetrologyModel(out HTuple hv_MetrologyHandle);
                        //增加一个矩形的计量模型
                        double[] hTuple = new double[] { hv_Rows1[0], hv_Columns1[0], hv_Rows1[hv_Rows1.Length - 1], hv_Columns1[hv_Columns1.Length - 1] };

                        //HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", hTuple, (int)pmaForm.numericUpDown17.Value, (int)pmaForm.numericUpDown16.Value, double.Parse(pmaForm.numericUpDown2.Value + ""), double.Parse(pmaForm.numericUpDown3.Value + ""), new HTuple(), new HTuple(), out HTuple hv_Index);

                        HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", hTuple, (int)form.line1HeightBox.Value, (int)form.line1Width.Value, double.Parse(form.line1muscle.Value + ""), double.Parse(form.line1contrast.Value + ""), new HTuple(), new HTuple(), out HTuple hv_Index);
                        string polarity = "";
                        if (form.line1Combox.SelectedItem.ToString() == "全部")
                        {
                            polarity = "all";
                        }
                        if (form.line1Combox.SelectedItem.ToString() == "正极性")
                        {
                            polarity = "negative";
                        }
                        if (form.line1Combox.SelectedItem.ToString() == "负极性")
                        {
                            polarity = "positive";
                        }
                        if (form.line1Combox.SelectedItem.ToString() == "仅有")
                        {
                            polarity = "uniform";
                        }
                        //设置极性
                        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_transition", polarity);
                        //卡尺的数量
                        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "num_measures", (int)form.line1NumberBox.Value);
                        //卡尺卡的数量 接受的结果数
                        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "num_instances", 1);
                        //bilinear
                        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_interpolation", "bicubic");
                        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_select", "all");
                        //最小分数
                        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "min_score", double.Parse(form.line1small.Value.ToString()));
                        //开始抓
                        HOperatorSet.ApplyMetrologyModel(HideIntputImage, hv_MetrologyHandle);
                        HOperatorSet.GetMetrologyObjectMeasures(out HObject ho_Contours, hv_MetrologyHandle, "all", "all",
                                    out HTuple hv_Row, out HTuple hv_Column);
                        //if (hv_Row.Length < 0)
                        //{

                        //    return;
                        //}
                        HOperatorSet.GenRegionLine(out HObject regionLine1, hv_Row[0], hv_Column[0], hv_Row[hv_Row.Length - 1], hv_Column[hv_Column.Length - 1]);
                        HOperatorSet.GenContourPolygonXld(out HObject regionLines1, new double[] { hv_Row[0].D, hv_Row[1].D }, new double[] { hv_Column[0].D, hv_Column[1].D });
                        if (form.comboBox4.Text == "开启")
                        {
                            HOperatorSet.DispObj(regionLines1, hw.HalconWindow);
                        }
                   
                        //清除线段显示
                        if (line1Object != null)
                        {
                            line1Object.ClearDrawingObject();
                        }

                        //创建线2的对象
                        HOperatorSet.GenRegionLine(out HObject hLine2, double.Parse(form.caliper2Row1.Value + ""), double.Parse(form.caliper2Col1.Value + ""), double.Parse(form.caliper2Row2.Value + ""), double.Parse(form.caliper2Col2.Value + ""));
                        //生成第一条跟随的线
                        HObject ho_RegionAffineTrans2 = UtiyHalcon.MatrixCalculate(hLine2, HideIntputArrayRows[i], HideIntputArrayColumns[i], HideIntputArrayPhis[i], line2counterFrist[0], line2counterFrist[1], line2counterFrist[2]);
                        HOperatorSet.GetRegionPoints(ho_RegionAffineTrans2, out HTuple hv_Rows2, out HTuple hv_Columns2);
                        if (hv_Rows2.Length == 0)
                        {
                            continue;
                        }
                        //创建计量模型，进行找线
                        HOperatorSet.CreateMetrologyModel(out HTuple hv_MetrologyHandle2);
                        //增加一个矩形的计量模型
                        double[] hTuple2 = new double[] { hv_Rows2[0], hv_Columns2[0], hv_Rows2[hv_Rows2.Length - 1], hv_Columns2[hv_Columns2.Length - 1] };

                        HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle2, "line", hTuple2, (int)form.line2HeightBox.Value, (int)form.line2Width.Value, double.Parse(form.line2muscle.Value + ""), double.Parse(form.line2contrast.Value + ""), new HTuple(), new HTuple(), out HTuple hv_Index2);
                        string polarity2 = "";
                        if (form.line2Combox.SelectedItem.ToString() == "全部")
                        {
                            polarity2 = "all";
                        }
                        if (form.line2Combox.SelectedItem.ToString() == "正极性")
                        {
                            polarity2 = "negative";
                        }
                        if (form.line2Combox.SelectedItem.ToString() == "负极性")
                        {
                            polarity2 = "positive";
                        }
                        if (form.line2Combox.SelectedItem.ToString() == "仅有")
                        {
                            polarity2 = "uniform";
                        }
                        //设置极性
                        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle2, "all", "measure_transition", polarity2);
                        //卡尺的数量
                        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle2, "all", "num_measures", (int)form.line2NumberBox.Value);
                        //卡尺卡的数量 接受的结果数
                        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle2, "all", "num_instances", 1);
                        //bilinear
                        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle2, "all", "measure_interpolation", "bicubic");
                        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle2, "all", "measure_select", "all");
                        //最小分数
                        HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle2, "all", "min_score", double.Parse(form.line2small.Value.ToString()));
                        //开始抓
                        HOperatorSet.ApplyMetrologyModel(HideIntputImage, hv_MetrologyHandle2);
                        HOperatorSet.GetMetrologyObjectMeasures(out HObject ho_Contours2, hv_MetrologyHandle2, "all", "all",
                                    out HTuple hv_Row2, out HTuple hv_Column2);
                        //清除线段显示
                        if (line2Object != null)
                        {
                            line2Object.ClearDrawingObject();
                        }
                        //生产一条线
                        HOperatorSet.GenRegionLine(out HObject regionLine2, hv_Row2[0], hv_Column2[0], hv_Row2[hv_Row2.Length - 1], hv_Column2[hv_Column2.Length - 1]);
                        HOperatorSet.GenContourPolygonXld(out HObject regionLines2, new double[] { hv_Row2[0].D, hv_Row2[1].D }, new double[] { hv_Column2[0].D, hv_Column2[1].D });
                        if (form.comboBox4.Text == "开启")
                        {
                            HOperatorSet.DispObj(regionLines2, hw.HalconWindow);
                        }
                        //计算线到线之间的距离  distance_ss    distanceMin:2条线段最小距离       distanceMax:2条线段最大距离
                        HOperatorSet.DistanceSs(hv_Row[0], hv_Column[0], hv_Row[hv_Row.Length - 1], hv_Column[hv_Column.Length - 1], hv_Row2[0], hv_Column2[0], hv_Row2[hv_Row2.Length - 1], hv_Column2[hv_Column2.Length - 1], out HTuple distanceMin, out HTuple distanceMax);
                        HideOutputArrayDistances[i] = distanceMin.D;
                        form.dataGridView1.Rows.Add(i, distanceMin.D);
                        //将数据给间距数组
                        linesList[i,0] = hv_Row[0];
                        linesList[i, 1] = hv_Column[0];
                        linesList[i, 2] = hv_Row2[0];
                        linesList[i, 3] = hv_Column2[0];
                        //生成一条测试的线段
                        HOperatorSet.AreaCenter(regionLine1,out HTuple area1,out HTuple rowLine1,out HTuple columnLine1);
                        HOperatorSet.AreaCenter(regionLine2, out HTuple area2, out HTuple rowLine2, out HTuple columnLine2);
                        HOperatorSet.GenContourPolygonXld(out HObject regionLines3, new double[] { rowLine1.D, rowLine2.D }, new double[] { columnLine1.D, columnLine2.D });
                        HOperatorSet.GenRegionLine(out HObject hObjectLine, rowLine1, columnLine1, rowLine2, columnLine2);
                        HOperatorSet.DispObj(regionLines3, hw.HalconWindow);
                        //显示文本
                        HOperatorSet.AreaCenter(hObjectLine, out HTuple area3, out HTuple rowLine3, out HTuple columnLine3);
                        HideOutputArrayRows = rowLine3;
                        HideOutputArrayColumns = columnLine3;
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("线段1抓取失败" + ex.Message);
                }
            }
        }
        /// <summary>
        /// 线与线中间放置卡尺
        /// </summary>
        public void Set_Line() {
            if (HideIntputImage == null)
            {
                throw new Exception("当前图像为空");
            }
            HWindow hv_WindowHandle = hw.HalconWindow;
            hv_WindowHandle.ClearWindow();
            hv_WindowHandle.DispObj(HideIntputImage);
            //清除线段显示
            if (line1Object != null&&line2Object!=null) {
                line1Object.ClearDrawingObject();
                line2Object.ClearDrawingObject();

                //设置halcon图像模式
                HOperatorSet.SetSystem("clip_region", "false");
                var line1Params = new HTuple(double.Parse(form.caliper1Row1.Value + ""), double.Parse(form.caliper1Col1.Value + ""), double.Parse(form.caliper1Row2.Value + ""), double.Parse(form.caliper1Col2.Value + ""));
                //使用线段1的实例来找线
                HOperatorSet.CreateMetrologyModel(out HTuple hv_MetrologyHandle);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", line1Params, double.Parse(form.line1HeightBox.Value.ToString()), double.Parse(form.line1Width.Value.ToString()),
                        double.Parse(form.line1muscle.Value.ToString()), double.Parse(form.line1contrast.Value.ToString()), new HTuple(), new HTuple(), out HTuple hv_Index);
                //该参数指定暗/亮或亮/暗边的使用  negative:正极性  positive:负极性  uniform:正负极性只有一个
                //获取极性
                string polarity = "";
                if (form.line1Combox.SelectedItem.ToString() == "全部")
                {
                    polarity = "all";
                }
                if (form.line1Combox.SelectedItem.ToString() == "正极性")
                {
                    polarity = "negative";
                }
                if (form.line1Combox.SelectedItem.ToString() == "负极性")
                {
                    polarity = "positive";
                }
                if (form.line1Combox.SelectedItem.ToString() == "仅有")
                {
                    polarity = "uniform";
                }
                //设置极性
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_transition", polarity);
                //卡尺的数量
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "num_measures", int.Parse(form.line1NumberBox.Value+""));
                //卡尺卡的数量 接受的结果数
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "num_instances", 1);
                //bilinear
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_interpolation", "bicubic");

                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_select", "all");
                //最小分数
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "min_score", double.Parse(form.line1small.Value.ToString()));

                //开始抓
                HOperatorSet.ApplyMetrologyModel(HideIntputImage, hv_MetrologyHandle);

                HOperatorSet.GetMetrologyObjectMeasures(out HObject ho_Contours, hv_MetrologyHandle, "all", "all",
                    out HTuple hv_Row, out HTuple hv_Column);
                int index = 0;
                if (hv_Row.Length > 0 && hv_Column.Length > 0)
                {

                    //得到距离在hv_Parameter中
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, 0, "all", "result_type", "all_param",
                        out HTuple hv_Parameter);
                    //没抓到
                    if (hv_Parameter.Length > 0)
                    {
                        //设置边框颜色
                        HOperatorSet.SetColor(hw.HalconWindow, form.textBox1.Text.ToLower());

                        //显示边框区域
                        HOperatorSet.DispObj(ho_Contours, hw.HalconWindow);

                        HOperatorSet.GetMetrologyObjectResultContour(out HObject ho_Contour, hv_MetrologyHandle, 0, "all", 1.5);
                        //中心线颜色
                        HOperatorSet.SetColor(hw.HalconWindow, form.textBox1.Text.ToLower());

                        HOperatorSet.SetLineWidth(hw.HalconWindow, 3);
                        HOperatorSet.DispObj(ho_Contour, hw.HalconWindow);
                        HOperatorSet.DistancePp(hv_Parameter[0], hv_Parameter
                            [1], hv_Parameter[2], hv_Parameter[3], out HTuple distance);
                        //添加结果集
                        HOperatorSet.SetLineWidth(hw.HalconWindow, 1);
                    }
                }


                //抓线段2
                line1Params = new HTuple(double.Parse(form.caliper2Row1.Value + ""), double.Parse(form.caliper2Col1.Value + ""), double.Parse(form.caliper2Row2.Value + ""), double.Parse(form.caliper2Col2.Value + ""));
                //使用线段1的实例来找线
                HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", line1Params, double.Parse(form.line2HeightBox.Value.ToString()), double.Parse(form.line2Width.Value.ToString()),
                        double.Parse(form.line2muscle.Value.ToString()), double.Parse(form.line2contrast.Value.ToString()), new HTuple(), new HTuple(), out hv_Index);
                //该参数指定暗/亮或亮/暗边的使用  negative:正极性  positive:负极性  uniform:正负极性只有一个
                //获取极性
                if (form.line2Combox.SelectedItem.ToString() == "全部")
                {
                    polarity = "all";
                }
                if (form.line2Combox.SelectedItem.ToString() == "正极性")
                {
                    polarity = "negative";
                }
                if (form.line2Combox.SelectedItem.ToString() == "负极性")
                {
                    polarity = "positive";
                }
                if (form.line2Combox.SelectedItem.ToString() == "仅有")
                {
                    polarity = "uniform";
                }
                //设置极性
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_transition", polarity);
                //卡尺的数量
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "num_measures", int.Parse(form.line2NumberBox.Value + ""));
                //卡尺卡的数量 接受的结果数
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "num_instances", 1);
                //bilinear
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_interpolation", "bicubic");

                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "measure_select", "all");
                //最小分数
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, "all", "min_score", double.Parse(form.line2small.Value.ToString()));

                //开始抓
                HOperatorSet.ApplyMetrologyModel(HideIntputImage, hv_MetrologyHandle);

                HOperatorSet.GetMetrologyObjectMeasures(out ho_Contours, hv_MetrologyHandle, "all", "all",
                    out hv_Row, out  hv_Column);

                if (hv_Row.Length > 0 && hv_Column.Length > 0)
                {

                    //得到距离在hv_Parameter中
                    HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, 0, "all", "result_type", "all_param",
                        out HTuple hv_Parameter);
                    //没抓到
                    if (hv_Parameter.Length > 0)
                    {
                        //设置边框颜色
                        HOperatorSet.SetColor(hw.HalconWindow, form.textBox1.Text.ToLower());

                        //显示边框区域
                        HOperatorSet.DispObj(ho_Contours, hw.HalconWindow);

                        HOperatorSet.GetMetrologyObjectResultContour(out HObject ho_Contour, hv_MetrologyHandle, 0, "all", 1.5);
                        //中心线颜色
                        HOperatorSet.SetColor(hw.HalconWindow, form.textBox1.Text.ToLower());

                        HOperatorSet.SetLineWidth(hw.HalconWindow, 3);
                        HOperatorSet.DispObj(ho_Contour, hw.HalconWindow);
                        HOperatorSet.DistancePp(hv_Parameter[0], hv_Parameter
                            [1], hv_Parameter[2], hv_Parameter[3], out HTuple distance);

                        HOperatorSet.SetLineWidth(hw.HalconWindow, 1);
                    }
                }
            }
        }
    }
}
